#include "debug.h"
#include "../DDIC/AllDDIC.h"

/********************************************************************************************
********************************************************************************************
    RM69310  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/

void RM69310_Write_Register		 	(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data);
void RM69310_Write_Register_Demura	(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data);
void RM69310_Read_Register		 	(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data);
void RM69310_Write_Gamma			 	(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data);
void RM69310_Gamma_OTP_Start		 	(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data);



void RM69310_Write_Register_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
   
    
}
void RM69310_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3Ïß 4Ïß8bit SPI Ä£Ê½
    {
        SPI_Write_code(SigMode,channel,buffer[3],buffer[4]);  
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        delay_ms(5);
        SSD2828_W_Array(SigMode,channel,buffer,2);
        delay_ms(50);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    }
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·µ»Ø Ð´¼Ä´æÆ÷×´Ì¬  £ºok  
}
void RM69310_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode == SPI3_Mode)          //3Ïß8bit SPI Ä£Ê½
    {

    }
    else if(SigMode == SPI4_Mode)          //4Ïß8bit SPI Ä£Ê½
    {
        switch(buffer[4]&0x0F)
        {
            case 0x00:      ////CMD1 read function
                buffer[4] = SPI4_Read_u8_Data(SigMode,channel,buffer[3]); 
                delay_ms(5);
                STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,(buffer[2])+3);           //·µ»Ø ¶ÁÈ¡µÄ ¼Ä´æÆ÷Êý¾Ý  £ºok  
                break;
            
            default :   ////CMD2 read function
                buffer[4] = SPI4_Read_u8_Data_page(SigMode,channel,buffer[4],buffer[3]); 
                delay_ms(5);
                STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,(buffer[2])+3);           //·µ»Ø ¶ÁÈ¡µÄ ¼Ä´æÆ÷Êý¾Ý  £ºok  
                break;
        }
        SPI_Write_code(SigMode,channel,0xfe,0x00);  
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
        else
            SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); 
        SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
        SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);                                    
        for(i=0;i<3;i++)
        {
            SSD2828_W_Cmd(SigMode,channel,0xBF);
            SSD2828_W_Data(SigMode,channel,buffer[3]);
            if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   
                SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);  
            delay_ms(25);
            buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);
        }                              
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        delay_ms(5);
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·µ»Ø ¶ÁÈ¡µÄ ¼Ä´æÆ÷Êý¾Ý  £ºok  	
    }
}

void RM69310_Read_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode == SPI3_Mode)          //3Ïß8bit SPI Ä£Ê½
    {

    }
    else if(SigMode == SPI4_Mode)          //4Ïß8bit SPI Ä£Ê½
    {

        {

            
 ////CMD2 read function
                buffer[3] = SPI4_Read_u8_Data_page(SigMode,channel,0x01,0xee); 
                delay_ms(5);
                buffer[4] = SPI4_Read_u8_Data_page(SigMode,channel,0x01,0xef); 
                delay_ms(5);
                STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,(buffer[2])+3);           //·µ»Ø ¶ÁÈ¡µÄ ¼Ä´æÆ÷Êý¾Ý  £ºok  

        }
        SPI_Write_code(SigMode,channel,0xfe,0x00);  
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
        else
            SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); 
        SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
        SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);                                    
        for(i=0;i<3;i++)
        {
            SSD2828_W_Cmd(SigMode,channel,0xBF);
            SSD2828_W_Data(SigMode,channel,buffer[3]);
            if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   
                SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);  
            delay_ms(25);
            buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);
        }                              
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        delay_ms(5);
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·µ»Ø ¶ÁÈ¡µÄ ¼Ä´æÆ÷Êý¾Ý  £ºok  	
    }
}


void RM69310_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3Ïß 4Ïß8bit SPI Ä£Ê½
    {
        SPI_Write_code(SigMode,channel,0xfe,0x02); ////0x02-->page  50;   0x03-->page 60
        for(i=0;i<0x9C;i++)
        {
            if((i != 0x2C)
                ||(i != 0x2E)
                ||(i != 0x3C)
                ||(i != 0x3E)
                ||(i != 0x57))
            SPI_Write_code(SigMode,channel,i,buffer[4+i]);  
        }
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        delay_ms(5);
        SSD2828_W_Array(SigMode,channel,buffer,2);
        delay_ms(50);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    }
		
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·µ»Ø Ð´¼Ä´æÆ÷×´Ì¬  £ºok  
 		
}


void RM69310_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode == SPI3_Mode)          //3Ïß8bit SPI Ä£Ê½
    {

    }
    else if(SigMode == SPI4_Mode)          //4Ïß8bit SPI Ä£Ê½
    {
        for(i=0;i<0x9C;i++)
        {
            buffer[4+i] = SPI4_Read_u8_Data_page(SigMode,channel,0x02,i); ////0x02-->page  50;   0x03-->page 60
            delay_ms(5);
            
        }
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,(buffer[2])+3);           //·µ»Ø ¶ÁÈ¡µÄ ¼Ä´æÆ÷Êý¾Ý  £ºok  
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
        else
            SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); 
        SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
        SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);                                    
        for(i=0;i<3;i++)
        {
            SSD2828_W_Cmd(SigMode,channel,0xBF);
            SSD2828_W_Data(SigMode,channel,buffer[3]);
            if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   
                SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);  
            delay_ms(25);
            buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);
        }                              
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        delay_ms(5);
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·µ»Ø ¶ÁÈ¡µÄ ¼Ä´æÆ÷Êý¾Ý  £ºok  	
    }
}

void RM69310_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	int index=0;
    
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3Ïß 4Ïß8bit SPI Ä£Ê½
    {

        SPI_Write_code(SigMode,channel,0xfe,0x00);

        SPI_Write_code(SigMode,channel,0x28,0x00);  ////display off
        delay_ms(50);
        SPI_Write_code(SigMode,channel,0xfe,0x01); //switch page
        
        index=0x00;
        if((buffer[4]&0x01)==0x01)//page 40
        {
                index += 0x20;
        }
        if((buffer[4]&0x02)==0x02)//page 50
        {
                index += 0x10;
        }
        if((buffer[4]&0x04)==0x04)//page 60
        {
                index += 0x08;
        }
        if((buffer[4]&0x08)==0x08)//page 70
        {
                index += 0x04;
        }
        SPI_Write_code(SigMode,channel,0xf0,index); ////otp pages  select
        
        SPI_Write_code(SigMode,channel,0xfe,0x01);
        SPI_Write_code(SigMode,channel,0xf2,0x03);
        SPI_Write_code(SigMode,channel,0xf3,0xa5);
        SPI_Write_code(SigMode,channel,0xf4,0x5a);
        SPI_Write_code(SigMode,channel,0xf5,0x3c);
        delay_ms(410);
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        delay_ms(5);
        SSD2828_W_Array(SigMode,channel,buffer,2);
        delay_ms(50);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    }
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·µ»Ø Ð´¼Ä´æÆ÷×´Ì¬  £ºok  
}

void RM69310_auto_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	
	if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3Ïß 4Ïß8bit SPI Ä£Ê½
    {

        SPI_Write_code(SigMode,channel,0xfe,0x02); //switch page  50
        
				SPI_Write_code(SigMode,channel,buffer[3],buffer[4]);
				SPI_Write_code(SigMode,channel,buffer[5],buffer[6]);
			
				SPI_Write_code(SigMode,channel,buffer[7],buffer[8]);
				SPI_Write_code(SigMode,channel,buffer[9],buffer[10]);
			
				SPI_Write_code(SigMode,channel,buffer[11],buffer[12]);
				SPI_Write_code(SigMode,channel,buffer[13],buffer[14]);

        delay_ms(1);
    }
		else if(SigMode == Mipi_Mode)
		{
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
	
	
			buffer1[0] = 0x02;
			buffer1[1] = 0xFE;
			buffer1[2] = 0x50;              // GAMMA1  page1
			SSD2828_W_Array(SigMode,channel,buffer1,0);	
			SSD2828_W_Reg(SigMode,channel,0xBC,0x02);
	
			for( i = 0;i<buffer[2];i++) 
			{	 
					SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|buffer[i+3]);  

					i++;
					delay_ms(2);
			}		
		
			SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		}
		else
		{
			
		}
	
		buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·µ»Ø Ð´¼Ä´æÆ÷×´Ì¬  £ºok  
		
}

void ProcessForIc52(  USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	

	switch(buffer[1]&0xFF)
	{
		case 0x01:                                      //写寄存器
				RM69310_Write_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x02:                                      //读寄存器
				RM69310_Read_Register(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x03:                                      //写Gamma
				RM69310_Write_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
		case 0x04:                                      //读Gamma
				RM69310_Read_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;
        case 0x05:                                      //读Gamma
				RM69310_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data); 
				break;
        case 0x06:                                      //读Gamma
				RM69310_Read_OTP_Start(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				case 0xDD:
				RM69310_auto_Gamma(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
				break;

		default:break;
	}			
}
	




/*************************************************************************************

*/
